//
//  VideoCoverViewController.m
//  OnionHome
//
//  Created by fengjie on 2019/10/15.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "VideoCoverViewController.h"

@interface VideoCoverViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;

/**<#属性#>*/
@property (nonatomic,strong)UIImageView*imageView;

/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;
@end

@implementation VideoCoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑封面";
    self.view.backgroundColor = KBlackColor;
    [self setNavTitleView];
    [self createUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
}
-(void)setUrl:(NSURL *)url{
    _url = url;
    ShowActivityHud(@"获取图片...");
    kWeakSelf(weakSelf);
    [Utilis getVideoImageWithVideoPath:url successfulBlock:^(NSMutableArray * _Nonnull array) {
        [weakSelf.dataArray addObjectsFromArray:array];
        [weakSelf.listView reloadData];
        if (weakSelf.dataArray.count > 0) {
            weakSelf.imageView.image = weakSelf.dataArray.firstObject;
        }
        HideHubView;
    }];
}
-(void)backBtnClicked{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setNavTitleView{
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :UIColorFromRGB(0xFFFFFF), NSFontAttributeName : BOLDSYSTEMFONT(19)}];

    [self addNavigationItemWithImageNames:@[@"back1"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
    button.titleLabel.font = SYSTEMFONT(14);
    button.frame = CGRectMake(0, 0, 58, 28);
    
    kWeakSelf(weakSelf);
    [button addActionHandler:^{
        [weakSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
-(void)createUI{
    self.imageView = UIImageView.new;
    self.imageView.backgroundColor = KGrayColor;
    
    UILabel * label = UILabel.new;
    label.textColor = UIColorFromRGB(0x959E97);
    label.font = SYSTEMFONT(14);
    label.text = @"左右滑动选取封面";
    
    [self.view addSubview:self.imageView];
    [self.view addSubview:label];
    [self.view addSubview:self.listView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.mas_equalTo(90);
        make.right.mas_equalTo(-28);
        make.bottom.mas_equalTo(-150);
    }];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.imageView.mas_bottom).offset(16);
    }];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.mas_equalTo(label.mas_bottom).offset(12);
        make.height.mas_equalTo(64);
        make.right.mas_equalTo(0);
    }];
}

#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VideoCoverCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(VideoCoverCell.class) forIndexPath:indexPath];
    cell.backgroundColor = KRedColor;
    cell.image = self.dataArray[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.imageView.image = self.dataArray[indexPath.row];
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = 64;
    return CGSizeMake(width, width);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.5;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0.5;
}

-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _listView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        [_listView registerClass:[VideoCoverCell class] forCellWithReuseIdentifier:NSStringFromClass(VideoCoverCell.class)];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
@end

@interface VideoCoverCell ()
/**<#属性#>*/
@property (nonatomic,strong)UIImageView*imageView;
@end


@implementation VideoCoverCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = UIImageView.new;
        [self addSubview:self.imageView];
        
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return self;
}
-(void)setImage:(UIImage *)image{
    _image = image;
    self.imageView.image = image;
}
@end
